package com.grantheath.erebus.view 
{
	import com.grantheath.erebus.view.interfaces.IErebusTransitionHandeler;
	import com.grantheath.erebus.model.enum.notifications.ErebusURLNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusPageNotifications;
	import com.grantheath.erebus.model.enum.names.ErebusMediatorNames;
	import com.grantheath.erebus.view.interfaces.IErebusPage;
	import com.grantheath.erebus.model.interfaces.IErebusPageNode;
	import com.grantheath.erebus.view.helpers.ErebusPageEvent;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * @author grantheath
	 */
	public class ErebusPageMediator extends Mediator implements IMediator 
	{
		public function ErebusPageMediator()
		{
			super(ErebusMediatorNames.PAGE_MEDIATOR);
		}

		override public function onRegister() : void
		{
			super.onRegister();
		}

		override public function listNotificationInterests() : Array
		{
			return [
				ErebusPageNotifications.SHOW_PAGE, 
				ErebusPageNotifications.HIDE_PAGE, 
				ErebusPageNotifications.CLEAR_PAGE_TRANSITION_EVENTS
			];
		}

		override public function handleNotification(notification : INotification) : void
		{
			var notificationName : String = notification.getName();
			var pageNode : IErebusPageNode = notification.getBody() as IErebusPageNode;
			
			if(notificationName == ErebusPageNotifications.HIDE_PAGE) {
				trace("hiding the page: " + pageNode);
				pageNode.page.transitionHandeler.addEventListener(ErebusPageEvent.TRANSITION_OUT_COMPLETE, onTransitionOutComplete);
				pageNode.page.transitionHandeler.transitionOut();
			} else if(notificationName == ErebusPageNotifications.SHOW_PAGE) {
				trace("showing the page: " + pageNode.page);
				pageNode.page.transitionHandeler.addEventListener(ErebusPageEvent.TRANSITION_IN_COMPLETE, onTransitionInComplete);
				pageNode.page.addEventListener(ErebusPageEvent.GET_PAGE, onGetPage);
				pageNode.page.transitionHandeler.transitionIn();
			} else if(notificationName == ErebusPageNotifications.CLEAR_PAGE_TRANSITION_EVENTS) {
				trace("clearing page transition events for node: " + pageNode);
				removePageEventListeners(pageNode);
			}
		}

		private function removePageEventListeners(pageNode : IErebusPageNode) : void
		{
			pageNode.page.removeEventListener(ErebusPageEvent.TRANSITION_IN_COMPLETE, onTransitionInComplete);
			pageNode.page.removeEventListener(ErebusPageEvent.TRANSITION_OUT_COMPLETE, onTransitionOutComplete);
		}
		
		//EVENT HANDELERS
		private function onTransitionInComplete(event : ErebusPageEvent) : void
		{
			var transitionHandeler : IErebusTransitionHandeler = event.target as IErebusTransitionHandeler;
			var page : IErebusPage = transitionHandeler.target as IErebusPage;
			var pageNode : IErebusPageNode = page.pageNode;
			
			sendNotification(ErebusPageNotifications.PAGE_SHOWN, pageNode);
		}

		private function onTransitionOutComplete(event : ErebusPageEvent) : void
		{
			var transitionHandeler : IErebusTransitionHandeler = event.target as IErebusTransitionHandeler;
			var page : IErebusPage = transitionHandeler.target as IErebusPage;
			var pageNode : IErebusPageNode = page.pageNode;
			
			sendNotification(ErebusPageNotifications.PAGE_HIDDEN, pageNode);
		}
		
		private function onGetPage(event : ErebusPageEvent) : void
		{
			sendNotification(ErebusURLNotifications.SET_PAGE, event.treePath);
		}
	}
}
